//
//  SYStatusPictureView.swift
//  YANAWeibo
//
//  Created by mac on 16/5/16.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit
import SnapKit

/// 重用标识
private let SYStatusPictureViewCellIdentifier = "SYStatusPictureViewCellIdentifier"
/// 间距
let itemMargin: CGFloat = 5
/// 每个图片的宽度
let itemWith = (ScreenWidth - 2 * StatusTableViewCellMargin - 2 * itemMargin) / 3

class SYStatusPictureView: UICollectionView,UICollectionViewDataSource {
    // 绑定数据方法
    var picUrls: [SYStatusPictureInfo]? {
        didSet {
            messageLabel.text = "\(picUrls?.count ?? 0)"
            let currentSize = calcSize(picUrls?.count ?? 0)
            self.snp_updateConstraints { (make) -> Void in
                make.size.equalTo(currentSize)
            }
            // 重新加载数据
            reloadData()
        }
    }
    // 懒加载label
    private lazy var messageLabel: UILabel = UILabel(textColor: RandomColor(), fontSize: 20)
    
    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowLayout = UICollectionViewFlowLayout()
        // 设置layout
        flowLayout.itemSize = CGSize(width: itemWith, height: itemWith)
        flowLayout.minimumInteritemSpacing = itemMargin
        flowLayout.minimumLineSpacing = itemMargin
        super.init(frame: frame, collectionViewLayout: flowLayout)
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        addSubview(messageLabel)
        
        //设置约束
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(self)
        }
        
        //注册cell
        registerClass(SYStatusPictureViewCell.self, forCellWithReuseIdentifier: SYStatusPictureViewCellIdentifier)
        //设置代理
        dataSource = self
    }
    
    private func calcSize(count: Int) -> CGSize {

        // 计算列数
        let cols = count > 3 ? 3 : count
        // 计算行数
        let rows = (count - 1) / 3 + 1
        
        var currentWidth = CGFloat(cols) * itemWith + CGFloat(cols - 1) * itemMargin
        let currentHeight = CGFloat(rows) * itemWith + CGFloat(rows - 1) * itemMargin
        if currentWidth < 0 {
            currentWidth = 0
        }
        return CGSize(width: currentWidth , height: currentHeight)
        
    }
    
     // MARK: uicollectionView数据源代理方法
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return picUrls?.count ?? 0
    }
    
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(SYStatusPictureViewCellIdentifier, forIndexPath: indexPath) as! SYStatusPictureViewCell
        
        cell.picInfo = picUrls![indexPath.item]
        return cell
    }
}

// 自定义配图cell
class SYStatusPictureViewCell: UICollectionViewCell {
    // 设置模型
    var picInfo: SYStatusPictureInfo? {
        didSet {
            guard let url = picInfo?.thumbnail_pic else {
                return
            }
             imageView.sd_setImageWithURL(NSURL(string: url), placeholderImage: UIImage(named: "timeline_image_placeholder"))
            gifImageView.hidden = !url.hasSuffix(".gif")
            
        }
    }
    
    // 懒加载图片
    private lazy var imageView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "timeline_image_placeholder"))
        //等比填充
        view.contentMode = UIViewContentMode.ScaleAspectFill
        view.clipsToBounds = true
        return view
    }()
    
    private lazy var gifImageView: UIImageView = UIImageView(image: UIImage(named: "timeline_image_gif"))
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(imageView)
        contentView.addSubview(gifImageView)
        imageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView).offset(UIEdgeInsetsZero)
        }
        gifImageView.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(imageView)
            make.bottom.equalTo(imageView)
        }
        
    }
    
}




